<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="busNumber">
        <el-input
          v-model="queryParams.busNumber"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路线" prop="routeName">
        <el-input
          v-model="queryParams.routeName"
          placeholder="请输入路线名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入司机名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆状态" prop="busStatus">
        <el-select v-model="queryParams.busStatus" placeholder="请选择车辆状态" clearable size="small">
          <el-option
            v-for="dict in busStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bus:car:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bus:car:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bus:car:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bus:car:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="主键" align="center" prop="busId" />-->
      <el-table-column label="司机" sortable align="center" prop="userName" />
      <el-table-column label="手机号" sortable align="center" prop="phonenumber" />
      <el-table-column label="车牌号" sortable align="center" prop="busNumber" />
      <el-table-column label="车辆颜色" sortable align="center" prop="busColor" />
      <el-table-column label="车辆型号" sortable align="center" prop="busModel" />
      <el-table-column label="满载人数" sortable align="center" prop="fullLoad" />
      <el-table-column label="所属路线" sortable align="center" prop="routeName" />
      <el-table-column label="车辆状态" sortable align="center" prop="busStatus" :formatter="busStatusFormat" />
<!--      <el-table-column label="车辆描述" align="center" prop="busInfo" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bus:car:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bus:car:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleView(scope.row)"
            v-hasPermi="['bus:car:remove']"
          >二维码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 二维码 -->
    <el-dialog :title="title" :visible.sync="openView" width="600px" append-to-body>
      <img :src="imgUrl" width="500px">
    </el-dialog>

    <!-- 添加或修改车辆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车牌号" prop="busNumber">
          <el-input v-model="form.busNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="司机" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择司机"  filterable >
            <el-option
              v-for="user in driverList"
              :key="user.userId"
              :label="user.userName"
              :value="user.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="路线" prop="routeId">
          <el-select v-model="form.routeId" placeholder="请选择路线"  filterable >
            <el-option
              v-for="route in routeList"
              :key="route.routeId"
              :label="route.routeName"
              :value="parseInt(route.routeId)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆颜色" prop="busColor">
          <el-input v-model="form.busColor" placeholder="请输入车辆颜色" />
        </el-form-item>
        <el-form-item label="满载人数" prop="fullLoad">
          <el-input-number size="small" v-model="form.fullLoad" :min="1" :max="200" label="请输入满载人数"></el-input-number>
        </el-form-item>
        <el-form-item label="车辆型号" prop="busModel">
          <el-input v-model="form.busModel" placeholder="请输入车辆型号" />
        </el-form-item>
        <el-form-item label="车辆状态">
          <el-radio-group v-model="form.busStatus">
            <el-radio
              v-for="dict in busStatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆描述" prop="busInfo">
          <el-input type="textarea" v-model="form.busInfo" placeholder="请输入车辆描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCar, getCar, delCar, addCar, updateCar, exportCar, listDriverUser, getQrCodeUrl } from "@/api/bus/car";
import { listRoute } from "@/api/bus/route";

export default {
  name: "Car",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆表格数据
      carList: [],
      driverList: [],
      routeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openView: false,
      imgUrl: "",
      // 车辆状态字典
      busStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        busNumber: null,
        userId: null,
        busColor: null,
        busInfo: null,
        busModel: null,
        busStatus: null,
        routeName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        busNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        busModel: [
          { required: true, message: "车辆型号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.busStatusOptions = response.data;
    });
    this.selectDriverUser();
    this.selectRouteList();
  },
  methods: {
    /** 查询车辆列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 车辆状态字典翻译
    busStatusFormat(row, column) {
      return this.selectDictLabel(this.busStatusOptions, row.busStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleView(row) {
      this.imgUrl = "";
      getQrCodeUrl(row.busId).then(res=>{
        console.log(res)
        this.imgUrl = res.msg;
        this.openView = true;
        this.title = row.busNumber;
      });
    },
    selectDriverUser(){
      listDriverUser().then(res => {
        this.driverList = res.data;
      })
    },
    selectRouteList(){
      let data = {"routeStatus":0};
      listRoute(data).then(res => {
        this.routeList = res.rows;
        console.log(this.routeList)
      });
    },
    // 表单重置
    reset() {
      this.form = {
        busId: null,
        busNumber: null,
        userId: null,
        busColor: null,
        busInfo: null,
        busModel: null,
        busStatus: 0,
        createTime: null,
        fullLoad: 1,
        routeName:null,
        routeId:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.busId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const busId = row.busId || this.ids
      getCar(busId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          for(let item of this.routeList){
            if(item.routeId === this.form.routeId){
              this.form.routeName = item.routeName;
            }
          }
          if (this.form.busId != null) {
            updateCar(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCar(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const busIds = row.busId || this.ids;
      this.$confirm('是否确认删除车辆编号为"' + busIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCar(busIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有车辆数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCar(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
